﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using UI.GraphService;
using BLL;
using BE;

namespace UI
{
	public partial class Options : System.Web.UI.Page
	{
		GraphServiceClient client = new GraphServiceClient();
		protected void Page_Load( object sender, EventArgs e )
		{
			try
			{
				RoomWorker rw = new RoomWorker();
				List<Room> Lrooms = new List<Room>();

				if ( Lrooms.Count != 0 )
				{
					UpdateText.Text = "There are some rooms in your system that has not yet been tied to any Locations<br />It's adviced that you tie all rooms to a location before updating the list of rooms and your map<br />rooms without location:<br />";
					foreach ( Room r in Lrooms )
					{
						UpdateText.Text += "<br />" + r.Name;
					}
				}
			}
			catch
			{

			}
			try
			{
				MiscSql sqlCom = new MiscSql();
				if ( sqlCom.TestConnection() )
				{
					ConnectionOutput.ForeColor = System.Drawing.Color.Green;
					ConnectionOutput.Text = "Connection to database is working";
				}
			}
			catch ( EasyException ex )
			{
				ConnectionOutput.ForeColor = System.Drawing.Color.Red;
				ConnectionOutput.Text = "There appears to be problems with the connection to the database, possible solutions:<br />";
				ConnectionOutput.Text += "check if the database is running through sql configuration manager<br />";
				ConnectionOutput.Text += "check if the server is logged on as an authorized sql user<br />";
				ConnectionOutput.Text += @"Create the file C:\weboptions\dbconfig.txt containing the text 'connectionstring=SqlServerAddress'<br />";
				ConnectionOutput.Text += "It is adviced that you contact your IT support staff to try and solve the issue<br />";
				ConnectionOutput.Text += "<br />Debug output:<br />";
				ConnectionOutput.Text += ex.Message + "<br />";
				ConnectionOutput.Text += ex.InnerException + "<br />";
				ConnectionOutput.Text += ex.Source + "<br />";
				ConnectionOutput.Text += ex.StackTrace + "<br />";
				ConnectionOutput.Text += ex.TargetSite + "<br />";
				ConnectionOutput.Text += ex.Data + "<br />";
				ConnectionOutput.Text += ex.HelpLink + "<br />";
			}
		}

		protected void UpdateRooms( object sender, EventArgs e )
		{
			client.UpdateRooms();
		}

		protected void UpdateMap( object sender, EventArgs e )
		{
			client.UpdateGraph();
		}
	}
}